package cn.oracle.eshop.auth.controller;

import cn.oracle.eshop.auth.domain.PriorityDTO;
import cn.oracle.eshop.auth.domain.PriorityVo;

import cn.oracle.eshop.auth.service.PriorityService;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author wyh
 * @ClassName ProrityController
 * @date 2022/1/12 /22:29
 */
@RestController
@RequestMapping("/auth/priority")
@Slf4j
public class ProrityController {
    /**
     * 权限管理services组件
     */
    @Autowired
    private PriorityService prorityService;

    /**
     * 查询根权限
     */
    @GetMapping("/root")
    public List<PriorityVo> listRootPropriorities() {
        try {
            List<PriorityDTO> priorityDTOS = prorityService.listRootPriorites();
            List<PriorityVo> priorityVos = new ArrayList<>(priorityDTOS.size());
            for (PriorityDTO priorityDTO : priorityDTOS) {
                priorityVos.add(priorityDTO.clone(PriorityVo.class));
            }
            return priorityVos;
        } catch (Exception e) {
            e.printStackTrace();
            log.error("error:{}", e);
        }
        return new ArrayList<>();
    }

    @GetMapping("/child/{parentId}")
    public List<PriorityVo> listChild(@PathVariable("parentId") long parentId) {
        List<PriorityDTO> priorityDTOS = prorityService.listChildPriorities(parentId);
        if (priorityDTOS == null) {
            priorityDTOS = new ArrayList<>();
        }
        List<PriorityVo> priorityVos = new ArrayList<>(priorityDTOS.size());
        for (PriorityDTO priorityDTO : priorityDTOS) {
            priorityVos.add(priorityDTO.clone(PriorityVo.class));
        }
        return priorityVos;

    }

    @GetMapping("/{id}")
    public PriorityVo getPriorityById(@PathVariable("id") Long id) {
        try {
            PriorityDTO prioritydto = prorityService.getPriorityById(id);
            if (prioritydto == null) {
                prioritydto = new PriorityDTO();
            }
            return prioritydto.clone(PriorityVo.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new PriorityVo();

    }

    @PostMapping("/save")
    public Boolean savePriority(@RequestBody PriorityVo priorityVo) {
        try {
            PriorityDTO prioritydto = priorityVo.clone(PriorityDTO.class);
            prorityService.savePriority(prioritydto);
        } catch (Exception e) {
            log.error("error:{}", e);
            return false;
        }
        return true;

    }

    /***
     *  更新权限
     * @author wyh
     * @date 2022/1/22
     * @param  priorityVo
     * @return boolean
     */

    @GetMapping("/update")
    public Boolean updatePriority(@RequestBody PriorityVo priorityVo) {
        try {
            PriorityDTO prioritydto = priorityVo.clone(PriorityDTO.class);
            prorityService.updatePriorityDo(prioritydto);
        } catch (Exception e) {
            log.error("error:{}", e);
            return false;
        }
        return true;

    }
    /**删除权限 */
    @DeleteMapping ("/{id}")
    public Boolean deletePriority(@PathVariable("id") Long id) {
        try {

          return   prorityService.removePriority(id);
        } catch (Exception e) {
            log.error("error:{}", e);

        }
        return false;

    }

}
